package com.campusevaluation.controller;

import cn.hutool.core.bean.BeanUtil;
import com.campusevaluation.common.exception.Result;
import com.campusevaluation.domain.dto.LoginDTO;
import com.campusevaluation.domain.dto.UserDTO;
import com.campusevaluation.domain.po.User;
import com.campusevaluation.domain.vo.UserVO;
import com.campusevaluation.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController("/users")
@Tag(name = "用户模块")
public class UserController {
    @Resource
    private IUserService userService;

    @Operation(summary = "登录接口")
    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody LoginDTO loginDTO) {
        return userService.login(loginDTO);
    }

    @Operation(summary = "注册接口")
    @PostMapping("/register")
    public Result<String> register(@RequestBody UserDTO registerDTO) {
        return userService.register(registerDTO);
    }

    @Operation(summary = "发送验证码")
    @PostMapping("/code")
    public Result<String> sendCode(@RequestBody UserDTO registerDTO) {
        return userService.sendVerificationCode(registerDTO);
    }

    @Operation(summary = "重置密码")
    @PostMapping("/reset")
    public Result<String> resetPassword(@RequestBody UserDTO registerDTO) {
        return userService.resetPassword(registerDTO);
    }

    @Operation(summary = "获取当前用户数据")
    @GetMapping("/get_data")
    public Result<UserVO> getUserData() {
        return userService.getUserData();
    }

    @Operation(summary = "更新用户信息")
    @PostMapping("/update")
    public Result<String> update(@RequestBody UserDTO registerDTO) {
        User user = userService.query().eq("id", registerDTO.getId()).one();
        String password = user.getPassword();
        BeanUtil.copyProperties(registerDTO, user);
        user.setPassword(password);
        userService.updateById(user);
        return Result.success(null, "用户数据修改成功");
    }

    @Operation(summary = "上传文件")
    @PostMapping("/upload")
    public Result<String> upload(@RequestBody MultipartFile file) {
        return userService.uploadFile(file);
    }

}
